use rust_mir_gen::{rules::r_fn, utils::mir_generator::generate_mir};
use std::{env, fs};

use std::io::{self, Write};
use syn::File;

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        eprintln!("Usage: {} <filename>", args[0]);
        std::process::exit(1);
    }
    let filename = &args[1];
    let input = fs::read_to_string(filename).expect("Failed to read file");
    let syntax: File = syn::parse_file(&input).unwrap();
    //println!("{:#?}", syntax);
    let fn_item = &syntax.items[0];
    let fn_item = match fn_item {
        syn::Item::Fn(f) => f.clone(),
        _ => {
            panic!("Expected a function")
        }
    };
    let t_result = r_fn::visit(fn_item);
    //println!("{:#?}", t_result);
    let all_codes = generate_mir(t_result);
    //println!("{:#?}", all_codes);
    let stdout = io::stdout();
    let mut handle = stdout.lock();

    for code in all_codes {
        writeln!(handle, "{}", code).unwrap();
    }
}
